package com.i2bt.util;

import android.content.Context;

import com.duowan.mobile.netroid.Listener;
import com.duowan.mobile.netroid.Network;
import com.duowan.mobile.netroid.Request;
import com.duowan.mobile.netroid.RequestQueue;
import com.duowan.mobile.netroid.cache.DiskCache;
import com.duowan.mobile.netroid.request.JsonObjectRequest;
import com.duowan.mobile.netroid.stack.HurlStack;
import com.duowan.mobile.netroid.toolbox.BasicNetwork;

import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import java.io.File;

public class HttpNetroidUtil {
	
	private static RequestQueue mRequestQueue;
	
//	private static ImageLoader mImageLoader;
//
//	private static FileDownloader mFileDownloader;
	
	private HttpNetroidUtil() {}
	
	public static void init(Context context) {
		if (mRequestQueue != null) {
			throw new IllegalStateException("initialized");
		}
		
		Network network = new BasicNetwork(new HurlStack(Const.USER_AGENT, null), HTTP.UTF_8);
		mRequestQueue = new RequestQueue(network, 4, new DiskCache(new File(
				context.getCacheDir(), Const.HTTP_DISK_CACHE_DIR_NAME), Const.HTTP_DISK_CACHE_SIZE));
		
		mRequestQueue.start();
	}
	
	public static void postRequest(String url, JSONObject content, Listener<JSONObject> listener) {
		JsonObjectRequest json = new JsonObjectRequest(Request.Method.POST, url, content ,listener);
		mRequestQueue.add(json);
	}
	
	class Const {
	    public static final int HTTP_MEMORY_CACHE_SIZE = 2 * 1024 * 1024; // 2MB
	    public static final int HTTP_DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB
	    public static final String HTTP_DISK_CACHE_DIR_NAME = "netroid";
	    public static final String USER_AGENT = "netroid.cn";
	}
	
}
